/**
 * 
 */
package com.ms.game.itembox;

import com.ms.game.itembox.db.PlayerCrystalChangeLogModel;
import com.ms.player.Player;
import com.ms.player.PlayerAttributeType;
import com.ms.player.UpdateAttributeList;
import com.ms.service.module.zone.command.BasicDebugCommand;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.datamanager.BaseDataInfo;
import com.xpec.c4.game.datamanager.ResultNo;
import com.xpec.c4.game.datamanager.agent.PlayerData;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.game.utility.GUIDFactory;
import com.xpec.c4.game.utility.GUIDType;

/**
 * 設定角色水晶數量的指令
 * 
 * @author TW15010
 */
public class SetCrystalCommand extends BasicDebugCommand {

	/**
	 * 變更前的水晶數量
	 */
	private int orgCrystal;

	public SetCrystalCommand() {
	}

	@Override
	public String execute() {
		if (commandValue.length != 2) {
			return getDescription();
		}

		int crystalCount = getParamToInt(1);
		if (crystalCount < 0) {
			return getDescription();
		}

		player = getPlayer();

		orgCrystal = player.getCrystal();
		player.setCrystal(crystalCount);

		UpdateAttributeList updateList = new UpdateAttributeList();
		updateList.addUpdateValue(PlayerAttributeType.CRYSTAL, player.getCrystal());
		
		BasicZoneModule module = getModule();
		if (module != null) {
			module.getPlayerDataAgent().updatePlayerDataToDC2(player, updateList, this,
					"updatePlayerDataToDCCallback", player);
		}

		return "proc ....";
	}

	public void updatePlayerDataToDCCallback(BaseDataInfo obj, Object... userObject) {
		Player player = (Player) userObject[0];
		PlayerData pd = (PlayerData) obj;

		// 寫 log
		if (pd.getResultCode() == ResultNo.OK) {
			PlayerCrystalChangeLogModel log = new PlayerCrystalChangeLogModel();

			GUID orderId = GUIDFactory.createUUID(GUIDType.TYPE_COMMAND_ORDER_ID);
			CrystalChangeWay cw = CrystalChangeWay.ADD_COMMAND_SET;

			log.setOrderId(orderId.toString());
			log.setAccountId(player.getAccountId().toString());
			log.setPlayerId(player.getGuid().toString());
			log.setPlayerName(player.getNameWithNumber());
			log.setModuleId(getModule().getModuleName());
			log.setChangeWay(cw.getId());
			log.setChangeWayDesc(cw.getDesc());
			log.setChangeCrystal(player.getCrystal());
			log.setBeforeCrystal(orgCrystal);
			log.setAfterCrystal(player.getCrystal());

			getModule().saveDbLog(log);
		}

		Player updatePlayer = pd.getPlayer();

		player.setCrystal(updatePlayer.getCrystal());
		player.sendUpdatedAttrToClient();
		BasicDebugCommand.sendDebugCommandResponse(player.getSessionID(), "ok");
	}

	@Override
	public String getName() {
		return "setcrystal";
	}

	@Override
	public String getDescription() {
		return getName() + " [count]";
	}

	@Override
	public String getShortDescription() {
		return "set player crystal count.";
	}
}
